package com.study.homework;

public class MyArrayList {

    private int[] array = new int[1 << 3];
    private int currIndex = 0;

    public void add(int data) {
        if (currIndex >= array.length - 1) {
            grownArray();
        }
        array[currIndex++] = data;
    }

    public int size() {
        return currIndex;
    }

    private void grownArray() {
        int[] newArray = new int[(int) (array.length * 1.5)];
        System.out.println("grown to" + newArray.length);

        for (int i = 0; i < array.length; i++) {
            newArray[i] = array[i];
        }
        array = newArray;
    }

    public int get(int index) {
        if (index < 0 || index > currIndex) {
            throw new ArrayIndexOutOfBoundsException();
        }
        return array[index];
    }
}
